package core.generator;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.jdt.internal.compiler.batch.Main;

import core.modeling.GerenciadorEntidades;
import core.modeling.metamodel.Classe;

public class GeradorDeCodigo {
	private static final String FILE_SEPARATOR = System
			.getProperty("file.separator");

//	private static final String PACKAGE = "jml";
	private static final String CLASSES = "classes";

	public static void compileJmlCode(String outputDir) {
		System.out.println("***************************************");
		System.out.println("Compilando o codigo JML gerado...");
		System.out.println("***************************************");

		List<String> compilerOptions = new ArrayList<String>();

		compilerOptions.add("-nowarn");
		compilerOptions.add("-d");
		compilerOptions.add(outputDir + FILE_SEPARATOR + CLASSES);

		File dir = new File(outputDir);
		for (File f : dir.listFiles()) {
			if (f.getName().endsWith(".java")) {
				compilerOptions.add(f.getAbsolutePath());
			}
		}

		String[] arr = new String[compilerOptions.size()];
		compilerOptions.toArray(arr);
		Main.main(arr);
	}

	public static void generateJmlCode(String outputDir) throws IOException {
		System.out.println("***************************************");
		System.out.println("Gerando codigo...");
		System.out.println("***************************************");

		File dir = new File(outputDir);
		if (!dir.exists())
			dir.mkdir();
		outputDir += FILE_SEPARATOR;
		dir = new File(outputDir);
		if (!dir.exists())
			dir.mkdir();

		for (Classe classe : GerenciadorEntidades.getInstance().getPacotes()
				.getAllClasses()) {
			if (classe.isTipoDefinidoPeloUsuario()) {
				File f = new File(outputDir + FILE_SEPARATOR + classe.getNome()
						+ ".java");

				if (!f.exists())
					f.createNewFile();

				BufferedWriter out = new BufferedWriter(new FileWriter(f));
//				out.write("package " + PACKAGE + ";");
//				out.write(ConstantesModelo.LINE_SEPARATOR);
//				out.write(ConstantesModelo.LINE_SEPARATOR);
				out.write(classe.generateJavaCode().toString());
				out.close();
			}
		}

		String[] jmlUtils = new String[] { "JMLFilter.java", "JMLSet.java" };
		for (String jmlUtil : jmlUtils) {
			File destino = new File(outputDir + FILE_SEPARATOR + jmlUtil);

			InputStream stream = GeradorDeCodigo.class.getClassLoader()
					.getResourceAsStream(jmlUtil);
			BufferedReader in = new BufferedReader(
					new InputStreamReader(stream));
			PrintStream p = new PrintStream(destino);
			in.readLine();
//			p.append("package " + PACKAGE + ";");
			while (true) {
				String linha = in.readLine();
				if (linha == null)
					break;
				p.append(linha + "\n");
			}
			p.flush();
			p.close();
			in.close();
		}

		System.out.println("Codigo gerado com sucesso na pasta '" + outputDir
				+ "'");
	}

	public static void main(String[] args) {
		compileJmlCode("outputSrc/gerado");
	}
}
